home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / colors.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  7KB  |  354 lines

  1. /* --------------------------------- colors.c ------------------------------- */
  2.  
  3. /* This is part of the flight simulator 'fly8'.
  4.  * Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  5. */
  6.  
  7. /* color handling.
  8. */
  9.  
  10. #include "fly.h"
  11.  
  12.  
  13. LOCAL_FUNC long NEAR
  14. color_show (Ulong c)
  15. {
  16.     return (((Ulong)C_RGB_R (c) << 16) |
  17.         (       C_RGB_G (c) <<  8) |
  18.         (       C_RGB_B (c)      ));
  19. }
  20.  
  21. #if 0
  22. LOCAL_FUNC long NEAR
  23. color_saturation (long color)            /* get intensity */
  24. {
  25.     int    r, g, b, w;
  26.  
  27.     r = C_RGB_R (color);
  28.     g = C_RGB_G (color);
  29.     b = C_RGB_B (color);
  30.  
  31.     w = (r*67+g*21+b*15)/(67+21+15);
  32.  
  33.     return (C_RGB (w, w, w));
  34. }
  35. #endif
  36.  
  37. LOCAL_FUNC long NEAR
  38. color_intensify (long color)            /* highlight a color */
  39. {
  40.     Uint    r, g, b, m;
  41.  
  42.     r = C_RGB_R (color);
  43.     g = C_RGB_G (color);
  44.     b = C_RGB_B (color);
  45.  
  46.     m = (r > g) ? r : g;
  47.     if (b > m)
  48.         m = b;
  49.     if (!m)
  50.         return (color);
  51.     
  52.     r = (Uint)muldiv (r, 0x0ff, m);
  53.     g = (Uint)muldiv (g, 0x0ff, m);
  54.     b = (Uint)muldiv (b, 0x0ff, m);
  55.  
  56.     return (C_RGB (r, g, b));
  57. }
  58.  
  59. #define COLOR_OFFSET    8
  60. #define COLOR_VALUE    6
  61. #define COLOR_SIZE    (COLOR_OFFSET+COLOR_VALUE+1)
  62. #define    COLOR_STEP    8
  63.  
  64. static char FAR names[][COLOR_SIZE] = {
  65.     "Black   xxxxxx",    /*  0 */
  66.     "Red     xxxxxx",    /*  1 */
  67.     "Blue    xxxxxx",    /*  2 */
  68.     "Magenta xxxxxx",    /*  3 */
  69.     "Green   xxxxxx",    /*  4 */
  70.     "Brown   xxxxxx",    /*  5 */
  71.     "Gray    xxxxxx",    /*  6 */
  72.     "dYellow xxxxxx",    /*  7 */
  73.     "Yellow  xxxxxx",    /*  8 */
  74.     "lRed    xxxxxx",    /*  9 */
  75.     "lBlue   xxxxxx",    /* 10 */
  76.     "lGray   xxxxxx",    /* 11 */
  77.     "spare1  xxxxxx",    /* 12 */
  78.     "skyBlue xxxxxx",    /* 13 */
  79.     "dGreen  xxxxxx",    /* 14 */
  80.     "White   xxxxxx"    /* 15 */
  81. };
  82.  
  83. static MENU MenuPalette[] = {
  84.     {'k', names[ 0]},    /*  0 */
  85.     {'r', names[ 1]},    /*  1 */
  86.     {'b', names[ 2]},    /*  2 */
  87.     {'m', names[ 3]},    /*  3 */
  88.     {'g', names[ 4]},    /*  4 */
  89.     {'n', names[ 5]},    /*  5 */
  90.     {'a', names[ 6]},    /*  6 */
  91.     {'h', names[ 7]},    /*  7 */
  92.     {'H', names[ 8]},    /*  8 */
  93.     {'o', names[ 9]},    /*  9 */
  94.     {'f', names[10]},    /* 10 */
  95.     {'A', names[11]},    /* 11 */
  96.     {'G', names[12]},    /* 12 */
  97.     {'s', names[13]},    /* 13 */
  98.     {'d', names[14]},    /* 14 */
  99.     {'w', names[15]},    /* 15 */
  100. {'\0', 0}};
  101.  
  102. extern char * FAR
  103. color_name (int color)
  104. {
  105.     static char    name[COLOR_OFFSET];
  106.  
  107.     memcpy (name, names[color], COLOR_OFFSET-1);
  108.     name[COLOR_OFFSET-1] = '\0';
  109.  
  110.     return (name);
  111. }
  112.  
  113. extern char * FAR
  114. color_rgb (int color)
  115. {
  116.     static char    value[COLOR_VALUE+1];
  117.  
  118.     memcpy (value, names[color]+COLOR_OFFSET, COLOR_VALUE);
  119.     value[COLOR_VALUE] = '\0';
  120.  
  121.     return (value);
  122. }
  123.  
  124. extern int FAR
  125. set_rrggbb (int letter, Ulong value)
  126. {
  127.     int    c;
  128.  
  129.     for (c = rangeof (MenuPalette); --c >= 0;) {
  130.         if (letter == MenuPalette[c].letter)
  131.             break;
  132.     }
  133.  
  134.     if (c < 0 || c >= rangeof (st.palette))
  135.         return (1);
  136.  
  137.     value = C_RGB (0x0ff&(Uint)(value>>16), 0x0ff&(Uint)(value>>8),
  138.             0x0ff&(Uint)value);
  139.     st.palette[c] = value;
  140.     sprintf (names[c]+COLOR_OFFSET, "%06lx", color_show (value));
  141.     if (ST_HFG == (Uint)c) {
  142.         value = color_intensify (value);
  143.         st.palette[ST_HFGI] = value;
  144.         sprintf (names[ST_HFGI]+COLOR_OFFSET, "%06lx",
  145.                             color_show (value));
  146.     }
  147.     return (0);
  148. }
  149.  
  150. extern void FAR
  151. set_palette (void)
  152. {
  153.     int    i;
  154.  
  155.     for (i = 0; i < rangeof (st.colors); ++i) {
  156.         sprintf (names[i]+COLOR_OFFSET, "%06lx",
  157.                         color_show (st.palette[i]));
  158.         if (Gr->SetPalette)
  159.             Gr->SetPalette (i, st.palette[i]);
  160.     }
  161. }
  162.  
  163. extern int FAR
  164. color_assign (Ushort *item)
  165. {
  166.     int    color;
  167.  
  168.     color = *item;
  169.     color = menu_open (MenuPalette, color);
  170.     if (MENU_FAILED != color)
  171.         menu_close ();        /* close MenuPalette */
  172.     if (color >= 0) {
  173.         *item = color;
  174.         sim_set ();
  175.         show_fixed (0);
  176.         sim_reset ();
  177.     }
  178.     return (color);
  179. }
  180.  
  181. static MENU MenuGetColor[] = {
  182.     {'+', "Brighter"},    /*  0 */
  183.     {'-', "Darker"},    /*  1 */
  184.     {'R', "+red"},        /*  2 */
  185.     {'r', "-red"},        /*  3 */
  186.     {'G', "+green"},    /*  4 */
  187.     {'g', "-green"},    /*  5 */
  188.     {'B', "+blue"},        /*  6 */
  189.     {'b', "-blue"},        /*  7 */
  190.     {'=', "New"},        /*  8 */
  191.     {'*', "Restore"},    /*  9 */
  192. {'\0', 0}};
  193.  
  194. LOCAL_FUNC void NEAR
  195. color_adjust (int c)
  196. {
  197.     Uint    r, g, b;
  198.     Ulong    newc;
  199.     int    sel;
  200.     long    l;
  201.     char    msg[7];
  202.     HMSG    *m;
  203.  
  204.     newc = st.palette[c];
  205.  
  206.     m = MsgWPrintf (0, names[c]);
  207.  
  208.     sel = 8;    /* new */
  209.     do {
  210.         if (m)
  211.             sprintf (m->text+COLOR_OFFSET, "%06lx",
  212.                 color_show (newc));
  213.         if (Gr->SetPalette) {
  214.             Gr->SetPalette (c, newc);
  215.             sim_set ();
  216.             show_fixed (0);
  217.             sim_reset ();
  218.         }
  219.  
  220.         r = C_RGB_R (newc);
  221.         g = C_RGB_G (newc);
  222.         b = C_RGB_B (newc);
  223.  
  224.         sel = menu_open (MenuGetColor, sel);
  225.  
  226.         switch (sel) {
  227.         default:
  228.         case MENU_ABORTED:
  229.         case MENU_FAILED:
  230.             break;
  231.         case 0:
  232.             if ((r += (r+COLOR_STEP-1)/COLOR_STEP) > 0x0ff)
  233.                 r = 0x0ff;
  234.             if ((g += (g+COLOR_STEP-1)/COLOR_STEP) > 0x0ff)
  235.                 g = 0x0ff;
  236.             if ((b += (b+COLOR_STEP-1)/COLOR_STEP) > 0x0ff)
  237.                 b = 0x0ff;
  238. set_newc:
  239.             newc = C_RGB (r, g, b);
  240.             break;
  241.         case 1:
  242.             r -= (r+COLOR_STEP-1)/COLOR_STEP;
  243.             g -= (g+COLOR_STEP-1)/COLOR_STEP;
  244.             b -= (b+COLOR_STEP-1)/COLOR_STEP;
  245.             goto set_newc;
  246.         case 2:
  247.             if ((r += (r+COLOR_STEP-1)/COLOR_STEP) > 0x0ff)
  248.                 r = 0x0ff;
  249.             goto set_newc;
  250.         case 3:
  251.             r -= (r+COLOR_STEP-1)/COLOR_STEP;
  252.             goto set_newc;
  253.         case 4:
  254.             if ((g += (g+COLOR_STEP-1)/COLOR_STEP) > 0x0ff)
  255.                 g = 0x0ff;
  256.             goto set_newc;
  257.         case 5:
  258.             g -= (g+COLOR_STEP-1)/COLOR_STEP;
  259.             goto set_newc;
  260.         case 6:
  261.             if ((b += (b+COLOR_STEP-1)/COLOR_STEP) > 0x0ff)
  262.                 b = 0x0ff;
  263.             goto set_newc;
  264.         case 7:
  265.             b -= (b+COLOR_STEP-1)/COLOR_STEP;
  266.             goto set_newc;
  267.         case 8:
  268.             sprintf (msg, "%06lx", color_show (newc));
  269.             edit_str ("color RRGGBB", msg, sizeof (msg));
  270.             if (1 == sscanf (msg, "%lx", &l)) {
  271.                 b = 0x0ff & (Uint)(l      );
  272.                 g = 0x0ff & (Uint)(l >> 8 );
  273.                 r = 0x0ff & (Uint)(l >> 16);
  274.                 goto set_newc;
  275.             } else
  276.                 MsgEPrintf (50, "*** Bad color");
  277.             break;
  278.         case 9:
  279.             newc = st.palette[c];
  280.             break;
  281.         }
  282.         if (MENU_FAILED != sel)
  283.             menu_close ();
  284.     } while (sel >= 0);
  285.  
  286.     st.palette[c] = newc;
  287.     memcpy (names[c], m->text, COLOR_SIZE);
  288.  
  289.     m = msg_del (m);
  290. }
  291. #undef COLOR_OFFSET
  292. #undef COLOR_STEP
  293.  
  294. /* this menu is used for programming the palette.
  295. */
  296. extern int FAR
  297. menu_palette (void)
  298. {
  299.     int    sel;
  300.  
  301.     for (sel = 0; MENU_ABORTED != sel;) {
  302.         sel = menu_open (MenuPalette, sel);
  303.         if (sel >= 0)
  304.             color_adjust (sel);
  305.         if (MENU_FAILED != sel)
  306.             menu_close ();
  307.     }
  308.     return (0);
  309. }
  310.  
  311. /* This menu allows assigning colors to image items.
  312. */
  313.  
  314. static MENU FAR MenuColors[] = {
  315.     {'i', "info"},        /*  0 */
  316.     {'m', "mfg"},         /*  1 */
  317.     {'w', "wfg"},        /*  2 */
  318.     {'c', "cfg"},         /*  3 */
  319.     {'h', "hud fg"},    /*  4 */
  320.     {'i', "hud fgi"},     /*  5 */
  321.     {'B', "hud bo"},     /*  6 */
  322.     {'l', "s left"},    /*  7 */
  323.     {'r', "s right"},     /*  8 */
  324.     {'b', "s both"},     /*  9 */
  325.     {'g', "ground"},     /* 10 */
  326.     {'1', "dull"},         /* 11 */
  327.     {'2', "faint"},     /* 12 */
  328.     {'3', "sky"},         /* 13 */
  329.     {'4', "friend"},     /* 14 */
  330.     {'5', "foe"},         /* 15 */
  331.     {'6', "help"},         /* 16 */
  332.     {'7', "fire1"},     /* 17 */
  333.     {'8', "fire2"},     /* 18 */
  334.     {'9', "menu"},      /* 19 */
  335.     {'0', "menuh"},     /* 20 */
  336. {'\0', 0}};
  337.  
  338. extern int FAR
  339. menu_colors (void)
  340. {
  341.     int    sel;
  342.  
  343.     for (sel = 0; sel >= 0;) {
  344.         sel = menu_open (MenuColors, sel);
  345.         if (MENU_FAILED == sel)
  346.             break;
  347.         if (sel >= 0)
  348.             color_assign (&st.assign[sel]);
  349.         menu_close ();                /* close MenuColors */
  350.     }
  351.  
  352.     return (MENU_FAILED == sel);
  353. }
  354.